package somar_test_hello.parser.rselV1_4a;
import java.util.Vector;
import org.xml.sax.ext.Attributes2Impl;
public class LookupVisitor extends Visitor{
	private boolean isTotalSearch;
	boolean isFindElement;
	private String elementName;
	private Vector<String> elementAttr;
	private Vector<EntryElement> elementSet;
	public LookupVisitor(String name, boolean total){
		this(name, null, total);
	}
	
	public LookupVisitor(Vector<String> attr, boolean total){
		this(null, attr, total);
	}
	
	public LookupVisitor(String name, Vector<String> attr, boolean total){
		isTotalSearch = total;
		isFindElement = false;
		elementName = name;
		elementAttr = attr;
		elementSet = new Vector<EntryElement>();
	}
	
	public void visit(ProcessElement node){
		search(node);
	}
	
	public void visit(ClasslibElement node){
		search(node);
	}
	
	public void visit(ClassElement node){
		search(node);
	}
	
	public void visit(VariableElement node){
		search(node);
	}
	
	public void visit(RSELElement node){
		search(node);
	}
	
	public void visit(InvokeElement node){
		search(node);
	}
	
	public void visit(DocumentationElement node){
		search(node);
	}
	
	public void visit(ToElement node){
		search(node);
	}
	
	public void visit(FromElement node){
		search(node);
	}
	
	public void visit(ParameterElement node){
		search(node);
	}
	
	public void visit(ReturnElement node){
		search(node);
	}
	
	public void visit(VariablesElement node){
		search(node);
	}
	
	public void visit(ImportElement node){
		search(node);
	}
	
	public void visit(AssignElement node){
		search(node);
	}
	
	public void search(EntryElement node){
		Vector<EntryElement> children;
		if(node.getQName().equals(elementName) && elementName != null){
			if(elementAttr == null){
				elementSet.add(node);
				isFindElement = true;
			}else{
				searchAttributes(node);
			}
			
		}else if(elementAttr != null && elementName == null){
			searchAttributes(node);
		}
		
		children = node.getChildren();
		for(int i=0; i<children.size(); i++){
			if((!isTotalSearch) && isFindElement)
				return;
			EntryElement eNode = children.get(i);
			eNode.accept(this);
		}
		
	}
	
	private void searchAttributes(EntryElement node){
		Attributes2Impl attrs = node.getAttribues();
		isFindElement = false;
		for(int j=0; j<elementAttr.size(); j++){
			for(int i=0; i<attrs.getLength(); i++){
				if(elementAttr.get(j).equals(attrs.getValue(i))){
					isFindElement = true;
					break;
				}else{
					isFindElement = false;
				}
				
			}
			
			if(!isFindElement)
				break;
		}
		
		if(isFindElement)
			elementSet.add(node);
	}
	
	public Vector<EntryElement> getFindElement(){
		return elementSet;
	}
	
}

